import { Course, CourseCatalog, CourseItem } from "@/types/course";
import { defineStore } from "pinia";

type CourseHistory = {
  courseItemId: string;
  catalogId: string;
};

type CurrentChapter = {
  catalogId: string;
  itemId: string;
};

export const useCourseStore = defineStore("course", {
  persist: true,
  state: () => ({
    liveUrl: "",
    isVIP: false,
    history: {
      courseItemId: "",
      catalogId: "",
    } as CourseHistory,

    // 目录记录
    catalogHistory: {} as { [key: Course["id"]]: { [key: string]: CourseCatalog["id"] | CourseItem["id"] } },
    // 记录当前章节信息
    currentChapter: {
      catalogId: "",
      itemId: "",
    } as CurrentChapter,
    // 是否跳转
    isSkip: false,
  }),
  actions: {
    setLiveUrl(url: string) {
      this.liveUrl = url;
    },

    updateHistory(history: CourseHistory) {
      this.history = history;
    },

    updateVIP(isVIP: boolean) {
      this.isVIP = isVIP;
    },

    updateCurrentChapter(currentChapter: CurrentChapter) {
      this.currentChapter = currentChapter;
    },

    updateSkip(isSkip: boolean) {
      this.isSkip = isSkip;
    },

    updateCatalogHistory(courseId: Course["id"], key: number | "item", id: CourseCatalog["id"] | CourseItem["id"]) {
      if (!this.catalogHistory[courseId]) {
        this.catalogHistory[courseId] = {};
      }
      this.catalogHistory[courseId][key] = id;
    },
  },
});
